import Vue from "vue";
import Router from "vue-router";
import _ from "lodash";

/* layout */
// const MainFrame = resolve => require(["@/layout/mainframe.vue"], resolve);

const Homepage = resolve => require(["@/views/homepage.vue"], resolve);
const Infopage = resolve => require(["@/views/info-page.vue"], resolve);
const Listpage = resolve => require(["@/views/list-page.vue"], resolve);
const TravelDynamics = resolve => require(["@/views/travel-dynamics.vue"], resolve);
const GovernmentAffairs = resolve => require(["@/views/government-affairs.vue"], resolve);
const BeautifulViews = resolve => require(["@/views/beautiful-views.vue"], resolve);
const Raiders = resolve => require(["@/views/raiders.vue"], resolve);
const Mailbox = resolve => require(["@/views/mailbox.vue"], resolve);
const Mail = resolve => require(["@/views/mail.vue"], resolve);
Vue.use(Router);

export const constantRouterMap = [
  {
    path: "/",
    component: Homepage,
    children: [
      { path: "/", name: "travel-dynamics", component: TravelDynamics },
      { path: "/government-affairs", name: "government-affairs", component: GovernmentAffairs },
      { path: "/beautiful-views", name: "beautiful-views", component: BeautifulViews },
      { path: "/raiders", name: "raiders", component: Raiders },
      { path: "/mailbox", name: "mailbox", component: Mailbox },
    ]
  },
  {
    name: "info-page",
    path: "/info-page",
    component: Infopage
  },
  {
    name: "list-page",
    path: "/list-page",
    component: Listpage
  },
  {
    name: "mail",
    path: "/mail",
    component: Mail
  },
]
// ========================================================
/* eslint-disable import/first */
import customRouteConfig from "./custom.route.config";
// import demoRouteConfig from "./demo/routers"

// 自定义合并算法
let customizer = function (objValue, srcValue) {
  // console.log(objValue, srcValue)
  if (_.isArray(objValue)) {
    // return _.concat(objValue, srcValue)
    // debugger
    return mergeArray(objValue, srcValue)
  }
}

let mergeArray = function (srcArray, distArray) {
  let result = []
  let pushedDist = []
  // console.log("----")
  _.forEach(srcArray, (src, index) => {
    let match = false
    _.forEach(distArray, (dist, idx) => {
      if (index === 0) {
        pushedDist.push(idx)
      }
      // path 相同 -- 合并
      if (src.path === dist.path) {
        _.mergeWith(src, dist, customizer)
        result.push(src)
        _.pull(pushedDist, idx)
        // console.log(pushedDist)
        match = true
        // return false
      } else if (index === srcArray.length - 1 && _.includes(pushedDist, idx)) {
        result.push(dist)
        _.pull(pushedDist, idx)
          // console.log(idx)
      }
    })
    if (!match) {
      result.push(src)
    }
  })
  return result
}

// resultConfig
// resultConfig = mergeArray(resultConfig, demoRouteConfig)

// export default resultConfig
export const resultConfig = mergeArray(constantRouterMap, customRouteConfig)

export default new Router({
  mode: "history",
  // base: __dirname,
  routes: resultConfig,
  scrollBehavior: () => ({ y: 0 })
})
